'''
Created on 20/03/2009

@author: eh01
'''
from xml.dom.minidom import parseString

class DST(object):
    '''
    classdocs
    '''
    __subject = None
    __predicate = None
    __object = None
    __literal = None
    __literal_type = None
    __opening_tag = "<"
    __closing_tag = "</"
    __end_tag = ">"
    __dst_tag_name = "dst"    
    __subject_tag_name = "subject"
    __predicate_tag_name = "predicate"
    __object_tag_name = "object"
    __literal_tag_name = "literal"
    __literal_type_tag_name = "literalType"
        
    def __init__(self, subject = None, predicate = None, object = None, literal = None, literal_type = None, xml_string = None):
        if xml_string is None:
            self.__subject = subject
            self.__predicate = predicate
            self.__object = object
            self.__literal = literal
            self.__literal_type = literal_type
        else:
            xml = parseString(xml_string)
            self.__subject = self.getValueForElement(self.__subject_tag_name, xml)
            self.__predicate = self.getValueForElement(self.__predicate_tag_name, xml)
            self.__object = self.getValueForElement(self.__object_tag_name, xml)
            self.__literal = self.getValueForElement(self.__literal_tag_name, xml)
            self.__literal_type = self.getValueForElement(self.__literal_type_tag_name, xml)
            
    def get_subject(self):
        return self.__subject


    def get_predicate(self):
        return self.__predicate


    def get_object(self):
        return self.__object


    def get_literal(self):
        return self.__literal


    def get_literal_type(self):
        return self.__literal_type


    def set_subject(self, value):
        self.__subject = value


    def set_predicate(self, value):
        self.__predicate = value


    def set_object(self, value):
        self.__object = value


    def set_literal(self, value):
        self.__literal = value


    def set_literal_type(self, value):
        self.__literal_type = value
    
    def to_xml(self):               
        xml = self.__opening_tag + self.__dst_tag_name + self.__end_tag + \
                self.get_subject_xml() + \
                self.get_predicate_xml() + \
                self.get_object_xml() + \
                self.get_literal_xml() + \
                self.get_literal_type_xml() + \
                self.__closing_tag + self.__dst_tag_name + self.__end_tag
        return xml
        
    def get_subject_xml(self):
        xml = self.__opening_tag + self.__subject_tag_name + self.__end_tag + \
                self.get_subject() + \
                self.__closing_tag + self.__subject_tag_name + self.__end_tag
        return xml 
    
    def get_predicate_xml(self):
        xml = self.__opening_tag + self.__predicate_tag_name + self.__end_tag + \
                self.get_predicate() + \
                self.__closing_tag + self.__predicate_tag_name + self.__end_tag
        return xml
    
    def get_object_xml(self):
        xml = self.__opening_tag + self.__object_tag_name + self.__end_tag
        if self.get_object():
            xml += self.get_object()
        xml += self.__closing_tag + self.__object_tag_name + self.__end_tag
        return xml

    def get_literal_xml(self):
        xml = self.__opening_tag + self.__literal_tag_name + self.__end_tag
        if self.get_literal():
            xml += str(self.get_literal())
        xml += self.__closing_tag + self.__literal_tag_name + self.__end_tag
        return xml

    def get_literal_type_xml(self):
        xml = self.__opening_tag + self.__literal_type_tag_name + self.__end_tag
        if self.get_literal_type():
            xml += self.get_literal_type()
        xml += self.__closing_tag + self.__literal_type_tag_name + self.__end_tag
        return xml      
            
    def getValueForElement(self, element_name, xml):
        if xml.getElementsByTagName(element_name):
            # assume there will be only one element
            element = xml.getElementsByTagName(element_name)[0]

            if element.childNodes:
                # assume there is only one child node for that element
                child_node = element.childNodes[0]
                
                if child_node.nodeType == child_node.TEXT_NODE:
                    return child_node.data
                else:
                    return None
            else:
                return None
        else:
            return None
                
    subject = property(get_subject, set_subject, None, None)

    predicate = property(get_predicate, set_predicate, None, None)

    object = property(get_object, set_object, None, None)

    literal = property(get_literal, set_literal, None, None)

    literal_type = property(get_literal_type, set_literal_type, None, None)
        